package batframework.mvc;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class Param
{
	private HttpServletRequest request;
	
	public <T> T getModel(Class<T> model)
	{
		Enumeration<String> names = request.getParameterNames();
		
		return null;
	}
	
	
	public String getParam(String name)
	{
		return request.getParameter(name);
	}
	
	public String[] getParams(String name)
	{
		return request.getParameterValues(name);
	}
	
	public Integer getInteger(String name)
	{
		return Integer.parseInt(getParam(name));
	}
	
	public Integer[] getIntegers(String name)
	{
		String[] params = getParams(name);
		Integer[] integers = new Integer[params.length];
		for (int i = 0; i < params.length; i++)
			integers[i] = Integer.parseInt(params[i]);
		return integers;
	}
	
	public Long getLong(String name)
	{
		return Long.parseLong(getParam(name));
	}
	
	public Long[] getLongs(String name)
	{
		String[] params = getParams(name);
		Long[] longs = new Long[params.length];
		for (int i = 0; i < params.length; i++)
			longs[i] = Long.parseLong(params[i]);
		return longs;
	}
	
	public Float getFloat(String name)
	{
		return Float.parseFloat(getParam(name));
	}
	
	/**
	 * default pattern yyyy-MM-dd
	 * @param date
	 * @return
	 */
	public Date getDate(String name)
	{
		return getDate("yyyy-MM-dd", name);
	}
	
	public Date getDate(String pattern,String name)
	{
		try {
			return new SimpleDateFormat(pattern).parse(getParam(name));
		} catch (ParseException e) {
			return null;
		}
	}

	public Param()
	{
	}

	public Param(HttpServletRequest request)
	{
		this.request = request;
	}
	
}
